#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import os
import wstage4
from fm_util import Util
from fm_param import FmConst


class Stage4WinXpBuilder:

    def __init__(self, infoPrinter, tmpDir):
        self._arch = wstage4.Arch.X86
        self._version = wstage4.Version.WINDOWS_XP
        self._edition = wstage4.Edition.WINDOWS_XP_PROFESSIONAL
        self._lang = wstage4.Lang.en_US
        self._isoFullfn = os.path.join(FmConst.mswinCacheDir, "windows-xp-professional.x86.en-us.iso")
        self._virtioIsoFulfn = os.path.join(FmConst.mswinCacheDir, "windows-xp-virtio.iso")
        self._p = infoPrinter

    def downloadFiles(self, bTitle):
        if bTitle:
            self._p.printInfo("- Downloading %s files..." % (Util.getOsName("mswin_xp")))

        os.makedirs(FmConst.mswinCacheDir, exist_ok=True)
        self._installIsoFile = wstage4.os_winxp.CloudInstallationMedia(self._arch, self._version, self._edition, self._lang, False,
                                                                       local_iso_filepath=self._isoFullfn,
                                                                       local_virtio_win_iso_filepath=self._virtioIsoFulfn)
        self._installIsoFile.download()

    def build(self, workDir):
        # step
        self._p.printInfo("- Initializing...")
        wdir = wstage4.WorkDir(workDir, reset=True)
        builder = None
        if True:
            s = wstage4.Settings()
            s.program_name = FmConst.programName
            s.verbose_level = 0

            ts = wstage4.os_winxp.TargetSettings()
            ts.arch = self._arch
            ts.version = self._version
            ts.edition = self._edition
            ts.lang = self._lang
            ts.product_key = "HCQ9D-TVCWX-X9QRG-J4B2Y-GR2TT"            # FIXME
            ts.user_name = "user0"
            ts.timezone_index = "085"                                   # FIXME
            ts.keyboard_layout = "US"                                   # FIXME

            builder = wstage4.Builder(s, ts, wdir)
            builder.action_prepare(self._installIsoFile)

        # step
        self._p.printInfo("- Installing windows...")
        builder.action_install_os()

        # step
        self._p.printInfo("- Installing applications...")
        builder.action_install_applications()

        # step
        self._p.printInfo("- Cleaning up...")
        builder.action_cleanup()

        # finish
        builder.finish()

        return wdir


class Stage4Win7Builder:

    def __init__(self, infoPrinter, tmpDir):
        self._arch = wstage4.Arch.X86_64
        self._version = wstage4.Version.WINDOWS_7
        self._edition = wstage4.Edition.WINDOWS_7_ULTIMATE
        self._lang = wstage4.Lang.en_US
        self._isoFullfn = os.path.join(FmConst.mswinCacheDir, "windows-7-ultimate.x86_64.en-US.iso")
        self._virtioIsoFulfn = os.path.join(FmConst.mswinCacheDir, "windows-7-virtio.iso")
        self._p = infoPrinter

    def downloadFiles(self, bTitle):
        if bTitle:
            self._p.printInfo("- Downloading %s files..." % (Util.getOsName("mswin_7")))

        os.makedirs(FmConst.mswinCacheDir, exist_ok=True)
        self._installIsoFile = wstage4.os_win7.CloudInstallationMedia(self._arch, self._version, self._edition, self._lang,
                                                                      local_iso_filepath=self._isoFullfn,
                                                                      local_virtio_win_iso_filepath=self._virtioIsoFulfn)
        self._installIsoFile.download()

    def build(self, workDir):
        # step
        self._p.printInfo("- Initializing...")
        wdir = wstage4.WorkDir(workDir, reset=True)
        builder = None
        if True:
            s = wstage4.Settings()
            s.program_name = FmConst.programName
            s.verbose_level = 0

            ts = wstage4.os_win7.TargetSettings()
            ts.arch = self._arch
            ts.version = self._version
            ts.edition = self._edition
            ts.lang = self._lang
            ts.product_key = "PPBK3-M92CH-MRR9X-34Y9P-7CH2F"             # FIXME
            ts.user_name = "user0"
            ts.timezone = "Eastern Standard Time"                        # FIXME
            ts.keyboard_layout = "US"                                    # FIXME
            ts.locale = "1033:00000409"                                  # FIXME

            builder = wstage4.Builder(s, ts, wdir)
            builder.action_prepare(self._installIsoFile)

        # step
        self._p.printInfo("- Installing windows...")
        builder.action_install_os()

        # step
        self._p.printInfo("- Installing applications...")
        builder.action_install_applications()

        # step
        self._p.printInfo("- Cleaning up...")
        builder.action_cleanup()

        # finish
        builder.finish()

        return wdir
